package com.atguigu.common.config.exception;

import com.atguigu.common.result.Result;
import org.apache.ibatis.jdbc.Null;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * className: GlobalExceptionHandler
 * description:
 * date: 2023/4/12 0012-23:15
 * <p>
 * project: guigu-oa-parent
 * package: com.atguigu.common.config.exception
 * email: 1085844536@qq.com
 * version:
 *
 * @author WangGuojian
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 全局异常处理执行的方法
     *
     * @param e {@link Exception}
     * @return {@link Result<Object>}
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result<Object> error(Exception e) {
        e.printStackTrace();
        return Result.fail().message("执行了全局异常处理...");
    }

    /**
     * 特定异常处理
     *
     * @param e {@link ArithmeticException}
     * @return {@link Result<Object>}
     */
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public Result<Object> error(ArithmeticException e) {
        e.printStackTrace();
        return Result.fail().message("执行了特定异常处理...");
    }

    /**
     * 自定义异常
     *
     * @param e {@link GuiguException}
     * @return {@link Result<Object>}
     */
    @ExceptionHandler(GuiguException.class)
    @ResponseBody
    public Result<Object> error(GuiguException e) {
        return Result.fail().code(e.getCode()).message(e.getMsg());
    }

    /**
     * 没有权限
     *
     * @param e {@link AccessDeniedException}
     * @return {@link Result<Null>}
     */
    @ExceptionHandler(AccessDeniedException.class)
    @ResponseBody
    public Result<Object> error(AccessDeniedException e) {
        return Result.fail().code(205).message("没有操作权限");
    }
}
